#include "stdafx.h"
#include "Fire.h"

Fire::Fire() : cObject()
{
	//...
	m_fDeltaTime = 0.0f;
}

void Fire::Init(char *szFileNFG, char *Fire, char *Mask, char *Disp)
{
	m_uiTexCount = 3;
	char **fileName = new char*[m_uiTexCount];
	fileName[0] = Fire;
	fileName[1] = Mask;
	fileName[2] = Disp;
	cObject::Init(GL_TEXTURE_2D, fileName, m_uiTexCount, szFileNFG);

	SAFE_DEL_ARRAY(fileName);
	//Initshader
	m_Shader = FireShader();
	m_Shader.Init("../Resources/Shaders/FireShaderVS.vs", "../Resources/Shaders/FireShaderFS.fs");
}

void Fire::Update(ESContext *esConText, GLfloat deltaTime)
{
	cObject::Update(esConText, deltaTime);
	m_fDeltaTime += deltaTime;
}

void Fire::Draw(ESContext *esContext)
{
	glEnable (GL_BLEND); 
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glUseProgram(m_Shader.program);
	glBindBuffer(GL_ARRAY_BUFFER, m_Model->GetVertexID());
	if(m_Shader.VertPosition != -1)
	{
		glEnableVertexAttribArray(m_Shader.VertPosition);
		glVertexAttribPointer(m_Shader.VertPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
	}
	if(m_Shader.VertTexCoord != -1)
	{
		glEnableVertexAttribArray(m_Shader.VertTexCoord);
		glVertexAttribPointer(m_Shader.VertTexCoord, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char*)0 + sizeof(Vector3));
	}

	if(m_Shader.WVPMatrix != -1)
	{
		Matrix maViewProjection = Globals::MainCamera->GetVPMatrix();
		glUniformMatrix4fv(m_Shader.WVPMatrix, 1, GL_FALSE, (GLfloat*)&(m_maLocalW * maViewProjection).m[0][0]);
	}

	if(m_Shader.Time != -1)
	{
		glUniform1f(m_Shader.Time, m_fDeltaTime);
	}

	for(int i=0; i<m_uiTexCount; i++)
	{
		if(m_Shader.Texture[i] != -1)
		{
			glActiveTexture(GL_TEXTURE0 + i);
			GLuint texID = m_Texture->GetTextureID()[i];
			glBindTexture(GL_TEXTURE_2D, texID);			
			glUniform1i(m_Shader.Texture[i], i);
		}
	}

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Model->GetIndiceID());
	glDrawElements(GL_TRIANGLES, m_Model->GetIndiceNumber(), GL_UNSIGNED_INT, 0);

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	glBindTexture(GL_TEXTURE_2D, 0);
}

Fire::~Fire()
{
}